﻿using System;
using System.IO;
using System.Media;

using Perseus;

namespace Hetymine.Executioner {
    public partial class Executioner {
        public static class Sounds {
            private static SoundPlayer _SoundPlayer = new SoundPlayer();
            public static void Error() {
                string soundLocation = Executioner.Config.GetString("Executioner.Sounds", "Error", string.Empty);
                Sounds.Error(soundLocation);
            }
            internal static void Error(string soundLocation) {
                if (!Sounds.Play(soundLocation)) {
                    SystemSounds.Hand.Play();
                }
            }
            public static void ErrorNotify() {
                string soundLocation = Executioner.Config.GetString("Executioner.Sounds", "Error Notify", string.Empty);
                Sounds.ErrorNotify(soundLocation);
            }
            internal static void ErrorNotify(string soundLocation) {
                if (!Sounds.Play(soundLocation)) {
                    SystemSounds.Beep.Play();
                }
            }
            public static void Success() {
                string soundLocation = Executioner.Config.GetString("Executioner.Sounds", "Success", string.Empty);
                Sounds.Success(soundLocation);
            }
            internal static void Success(string soundLocation) {
                if (!Sounds.Play(soundLocation)) {
                    Stream stream = App.GetResourceStream(new Uri("pack://application:,,,/Resources/Sounds/success.wav")).Stream;
                    Sounds.Play(stream);
                }
            }
            public static void SuccessNotify() {
                string soundLocation = Executioner.Config.GetString("Executioner.Sounds", "Success Notify", string.Empty);
                Sounds.SuccessNotify(soundLocation);
            }
            internal static void SuccessNotify(string soundLocation) {
                if (!Sounds.Play(soundLocation)) {
                    Stream stream = App.GetResourceStream(new Uri("pack://application:,,,/Resources/Sounds/success_notify.wav")).Stream;
                    Sounds.Play(stream);
                }
            }

            private static bool Play(string soundLocation) {
                if (!soundLocation.IsEmpty() && File.Exists(soundLocation)) {
                    try {
                        Sounds._SoundPlayer.SoundLocation = soundLocation;
                        Sounds._SoundPlayer.Play();
                        return true;
                    }
                    catch { }
                }

                return false;
            }
            private static bool Play(Stream stream) {
                try {
                    Sounds._SoundPlayer.Stream = stream;
                    Sounds._SoundPlayer.Play();
                    return true;
                }
                catch {
                    return false;
                }
            }

            private static void Stop() {
                Sounds._SoundPlayer.Stop();
            }
        }
    }
}
